/**
 * 功能描述 收费行编辑
 */

define(function(require, exports, module) {
	var EditGrid=require("editgrid");
	var enmu = require("enums");
	var store =require("store");
	var activeUser = store.get("user");
	var Dialog = require("dialog");
	var applyGridEvents = require("../handler/applyGridEvents");

	var instance = {
		initComponent : function(params, widget) {
			var pereditgrid = new EditGrid({
						parentNode : ".J-pereditgrid",
						autoRender:false,
						model : {
							id : "memberRegisterApplyGrid",
							onAdd: function(editors) {
								var roomType = editors["roomType.name"];
					            var pkRoomType = roomType.getValue();
					            var count = editors["count"].getValue();
					            var payables = editors["payables"].getValue();
					            
					            if(pkRoomType && count && payables){
					            	pereditgrid.add({
					            		roomType:roomType.getData(pkRoomType),
					            		count:count,
					            		payables:payables,
					            	});
					            	return true;
					            }else{
					            	return false;
					            }
					         },
							columns : [{
								name : "roomType.name",
								label : "房型",
								editor:{
									type:"select",
									placeholder: "请输选择房型",
									url: "api/roomtype/query",
									params:function(){
										var obj={
											pkServicePoint:activeUser.pkServicePoint,
											fetchProperties:"pkRoomType,name,vesion"	
										}
										return obj;
									},
									keyField:"pkRoomType",
									valueField:"name",
									onEdit:function(editor,rowIndex,rowData){
										editor.setValue(rowData.roomType.name);
									},
									onChange:function(plugin,index,rowData){
										rowData.roomType =plugin.getData(plugin.getValue());
										pereditgrid.update(index,rowData);
									}
								}
							},{
								name : "count",
								label : "数量",
								editor:{
									type:"text",
									placeholder: "请输入数量",
									onEdit:function(editor,rowIndex,rowData){
										var reg = /^[0-9]*$/;
										if(!reg.test(rowData.count)){
					            			Dialog.alert({
					            				content:"请输入正确数量！"
					            				});	
					            		}else{
					            			editor.setValue(rowData.count);
					            		}
									},
									onChange:function(plugin,index,rowData){
										rowData.count = plugin.getValue();
										pereditgrid.update(index,rowData);
									}
								}
							},{
								name : "payables",
								label : "入住单价",
								editor:{
									type:"text",
									placeholder: "请输入单价",
									onEdit:function(editor,rowIndex,rowData){
										editor.setValue(rowData.payables);
									},
									onChange:function(plugin,index,rowData){
										rowData.payables = plugin.getValue();
										pereditgrid.update(index,rowData);
									}
								}
							},{
								name : "show",
								label : "操作",
								format : "button",
								formatparams : [{
									id : "delete",
//									icon : "icon-remove",
									text:"删除",
									handler : function(index, data, rowEle) {
										applyGridEvents.deleteApply(data,widget,index);
									}
								} ]
							} ]
						}
					});
			widget.set("pereditgrid", pereditgrid);
		}
	};
	module.exports = instance;
});
